package com.hd.rcugrc.project.leaveapplication.leave.operator;

import com.hd.rcugrc.bpm.*;
import com.hd.rcugrc.project.leaveapplication.annualleave.dao.OaOfficeAnnualLeaveDao;
import com.hd.rcugrc.project.leaveapplication.leave.dao.OaOfficeLeaveDao;
import com.hd.rcugrc.project.leaveapplication.leave.entity.OaOfficeLeaveEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OaOfficeLeaveOperator implements ExecutionTask {
    @Autowired
    private OaOfficeLeaveDao dao;
    @Override
    public ActionResult execute(ActiveStep activeStep, String actionId,
                                Map<String, List<Operator>> successors,
                                FlowInstance flowInstance, FlowDefinition flowDefinition,
                                User caller, Map<String, Object> context) {
        long id = flowInstance.getId();
        OaOfficeLeaveEntity entity = dao.findByInstId(id);
        long kssj = entity.getQjsqdKssj();
        long jssj = entity.getQjsqdJssj();
        long nowTime = System.currentTimeMillis();
        if(nowTime > kssj && nowTime < jssj) {
            entity.setQjsqdKssj(nowTime);
        }
        if(nowTime > jssj) {
            entity.setQjsqdQjts(0);
        }
        //时间格式,HH是24小时制，hh是AM PM12小时制
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date_string = sdf.format(new Date(entity.getQjsqdKssj() ));
        //至于取10位或取13位，date_temp*1000L就是这种截取作用。如果是和服务器传值的，就和后台商量好就可以了
        entity.setTitle("请假申请" + "-"+date_string +"-"+ entity.getQjsqdJb());
        dao.mergeAndFlush(entity);
        return null;
    }
}

